package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.config.redis.redis.util.Redishandler0;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.entity.MessageNotice;
import cn.stylefeng.guns.modular.business.entity.ReadMessage;
import cn.stylefeng.guns.modular.business.mapper.MessageNoticeMapper;
import cn.stylefeng.guns.modular.business.mapper.ReadMessageMapper;
import cn.stylefeng.guns.modular.business.model.params.ReadMessageDTO;
import cn.stylefeng.guns.modular.business.model.result.MessageNumResult;
import cn.stylefeng.guns.modular.business.service.ReadMessageService;
import cn.stylefeng.guns.sys.core.util.StringUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 已读消息表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-07-30
 */
@Service
public class ReadMessageServiceImpl extends ServiceImpl<ReadMessageMapper, ReadMessage> implements ReadMessageService {

    @Autowired
    Redishandler0 redishandler0;

    @Autowired
    MessageNoticeMapper messageNoticeMapper;

    @Override
    public void add(ReadMessageDTO param, Long userId) {
        ReadMessage readMessage = baseMapper.selectOne(new QueryWrapper<ReadMessage>().eq("message_notice_id", param.getMessageNoticeId())
                .eq("user_id", userId));
        if (readMessage == null) {
            ReadMessage entity = new ReadMessage();
            ToolUtil.copyProperties(param, entity);
            entity.setUserId(userId);
            save(entity);
            //保存缓存
            Set<String> ids = redishandler0.getSet(Constants.READ_MESSAGE_KEY, userId.toString() + param.getType().toString());
            ids.add(entity.getMessageNoticeId());
            if (ids.size() > 0) {
                redishandler0.cacheSet(Constants.READ_MESSAGE_KEY, userId.toString() + param.getType().toString(), ids);
                long a = redishandler0.incr(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + param.getType().toString(), -1);
                if (a < 0) {
                    redishandler0.remove(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + param.getType().toString());
                }
            }
        }
    }

    @Override
    public void addAllUnreadItem(Long userId) {
        //所有系统消息
        List<MessageNotice> messageNotices = messageNoticeMapper.selectList(new QueryWrapper<MessageNotice>().eq("type", 1)
                .eq("user_id", userId));
        Set<String> systemMsgIds = messageNotices.stream().map(MessageNotice::getId).collect(Collectors.toSet());
        Set<String> ids = redishandler0.getSet(Constants.READ_MESSAGE_KEY, userId.toString() + 1);
        systemMsgIds.addAll(ids);
        if (systemMsgIds.size() > 0) {
            redishandler0.cacheSet(Constants.READ_MESSAGE_KEY, userId.toString() + 1, systemMsgIds);
        }
        redishandler0.remove(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() +1);

        //所有平台消息
        List<MessageNotice> messages = messageNoticeMapper.selectList(new QueryWrapper<MessageNotice>().eq("type", 2));
        Set<String> platformMsgIds = messages.stream().map(MessageNotice::getId).collect(Collectors.toSet());
        Set<String> platformReadMsgIds = redishandler0.getSet(Constants.READ_MESSAGE_KEY, userId.toString() + 2);
        platformMsgIds.addAll(platformReadMsgIds);
        if (platformMsgIds.size() > 0) {
            redishandler0.cacheSet(Constants.READ_MESSAGE_KEY, userId.toString() + 2, platformMsgIds);
        }
        redishandler0.remove(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + 2);
    }


    /**
     * 获取未读消息数量
     *
     * @param userId
     * @return
     */
    @Override
    public MessageNumResult getUnReadNum(Long userId) {
        MessageNumResult messageNumResult = new MessageNumResult();
        String systemUnreadMessageCountStr = redishandler0.getStringValue(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + 1);
        String platformUnreadMessageCountStr = redishandler0.getStringValue(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + 2);
        Set<String> ids = redishandler0.getSet(Constants.READ_MESSAGE_KEY, userId.toString() + 2);
        Integer systemUnreadMessageCount = StringUtils.isNotEmpty(systemUnreadMessageCountStr) ? Integer.valueOf(systemUnreadMessageCountStr) : 0;
        Integer platformUnreadMessageCount = StringUtils.isNotEmpty(platformUnreadMessageCountStr) ? Integer.valueOf(platformUnreadMessageCountStr) : 0;
        long messageCount = messageNoticeMapper.selectCount(new QueryWrapper<MessageNotice>().eq("type", 2));
        if (messageCount > ids.size()) {
            messageNumResult.setPlatformUnreadMessageCount(Long.valueOf(messageCount - ids.size()).intValue());
        } else {
            messageNumResult.setPlatformUnreadMessageCount(platformUnreadMessageCount);
        }
        messageNumResult.setSystemUnreadMessageCount(systemUnreadMessageCount);
        return messageNumResult;
    }

}
